/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.jmex.model.ogredotscene.loader;

import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.state.LightState;
import com.jme.scene.state.RenderState.StateType;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jmex.model.ogredotscene.gen.Environment;
import com.jmex.model.ogredotscene.gen.SkyBox;
import com.jmex.model.ogredotscene.util.XMLUtil;
import java.util.HashMap;
import java.util.Map;
import com.jme.scene.Skybox;

/**
 *
 * @author czarnik_a
 */
public class EnviromentLoader {

    private LightState lightState;
    private Map<String, Material> materials = new HashMap<String, Material>();
    private com.jme.scene.Node jmeScene;
    private com.jme.scene.Skybox jmeSkybox;

    public void setEnviroment(Environment environment,com.jme.scene.Node jmeSceneNode) {

         if (lightState == null) {
            throw new IllegalStateException("Light state is not set up yet");
        }

        if (environment.getColourAmbient() != null){
            ColorRGBA globalAmbientColor = new ColorRGBA();
            globalAmbientColor.r = XMLUtil.getFloatString(environment.getColourAmbient().getR());
            globalAmbientColor.g = XMLUtil.getFloatString(environment.getColourAmbient().getG());
            globalAmbientColor.b = XMLUtil.getFloatString(environment.getColourAmbient().getB());
            lightState.setGlobalAmbient(globalAmbientColor);
        }

        if (environment.getColourBackground() != null){
            ColorRGBA globalAmbientColor = new ColorRGBA();
            globalAmbientColor.r = XMLUtil.getFloatString(environment.getColourAmbient().getR());
            globalAmbientColor.g = XMLUtil.getFloatString(environment.getColourAmbient().getG());
            globalAmbientColor.b = XMLUtil.getFloatString(environment.getColourAmbient().getB());
             DisplaySystem.getDisplaySystem().getRenderer().setBackgroundColor(globalAmbientColor);
        }

         if (environment.getSkyBox() != null) {
             Material material = materials.get(environment.getSkyBox().getMaterialName());
             jmeSkybox = new com.jme.scene.Skybox(environment.getSkyBox().getName(), 512, 512, 512);
             Skybox.Face skyboxOder[] = {Skybox.Face.Up, Skybox.Face.Down, Skybox.Face.East, Skybox.Face.West, Skybox.Face.South, Skybox.Face.North};
             int unitTexture = 0;
             for (Skybox.Face face : skyboxOder) {
                 TextureState textureState = (TextureState) material.getRenderState(StateType.Texture);
                 jmeSkybox.setTexture(face,textureState.getTexture(unitTexture));
                 unitTexture += 1;
             }
             jmeSkybox.preloadTextures();
             jmeSceneNode.attachChild(jmeSkybox);
         }
    }

    public LightState getLightState() {
        return lightState;
    }

    public void setLightState(LightState lightState) {
        this.lightState = lightState;
    }
    
    public Map<String, Material> getMaterials() {
        return materials;
    }

    public void setMaterials(Map<String, Material> materials) {
        this.materials = materials;
    }

    public Node getJmeScene() {
        return jmeScene;
    }

    public void setJmeScene(Node jmeScene) {
        this.jmeScene = jmeScene;
    }

    public Skybox getJmeSkybox() {
        return jmeSkybox;
    }

    public void setJmeSkybox(Skybox jmeSkybox) {
        this.jmeSkybox = jmeSkybox;
    }



}
